// components/InputModal/InputModal.js
import { request } from "../../utils/util.js"
const { $Message } = require('../../dist/base/index');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    parent: {
      type: String,
      value: ""
    },
    type: {
      type: String,
      value: ""
    },
    title: {
      type: String,
      value: "新建文件夹"
    },
    fileId: {
      type: Number,
      value: ""
    },
    key: {
      type: Number,
      value: ""
    },
    fileType: {
      type: String,
      value: ""
    },
    name: {
      type: String,
      value: ""
    },
    project:{
      type:Number
    }//新建的项目目录要传
  },

  /**
   * 组件的初始数据
   */
  data: {
    visibleCreate: false,
    folderValue: "",
    classTip: "toolTip-hidden",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 父传子方法
     */
    setShow: function () {

      this.setData({
        ...this.data,
        visibleCreate: true
      })
    },
    
    /**
   * 创建文件夹
   * @param {*} e 
   */
    handleCreate: function () {
      const that = this
      
      if (that.data.folderValue.length > 0) {
        let param = {}
        if(this.properties.type === "project"){
          param = {
            title: this.data.folderValue,
            type: this.properties.type || "",
            parent: this.properties.parent || "",
            project:this.properties.project || ""
          }
        }else{
           param = {
            title: this.data.folderValue,
            type: this.properties.type || "",
            parent: this.properties.parent || ""
          }
        }
        
        request("POST", `/api/service/catalog/`, param)
          .then(res => {
            if(res.statusCode === 427){
              that.setData({
                ...that.data,
                folderValue: "",
                visibleCreate: false
              })
              that.triggerEvent("handleCancel1")
            }
            if (res.statusCode === 200) {
              that.setData({
                ...that.data,
                folderValue: "",
                visibleCreate: false
              })
              $Message({
                content: "创建成功",
                type: 'success'
              });
              that.triggerEvent("getFileList");
              that.triggerEvent("handleCancel1")
             
            }
          })

      } else {
        this.setData({
          ...this.data,
          classTip: "toolTip-show"
        })
      }
    },
    /**
     * 创建文件夹里的取消按钮
     * @param {*} e 
     */
    handleCancel: function () {
      this.setData({
        ...this.data,
        folderValue: "",
        classTip: "toolTip-hidden",
        visibleCreate: false
      })
    },
    /**
  * 创建里的Input
  */
    createInput(e) {
      this.setData({
        ...this.data,
        folderValue: e.detail.value
      })
      if (e.detail.value != "") {
        this.setData({
          ...this.data,
          classTip: "toolTip-hidden"
        })
      }
    },
  }
})
